// pages/user/user/bindTelStepOne/bindTelStepOne.js
const app = getApp();
import Api from '../../../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: app.globalData.userInfo,
    codeText: '获取验证码',
    zhengLove: true,
    verifyCode: '', //验证码
    codeTime: 60, //验证码时间
    // mobile: '',
  },
  //下一步
  formSubmit(e) {
    let that = this;
    if (e.detail.phoneCode === '') {
      wx.showModal({
        title: "提示",
        content: "请输入正确的验证码",
        showCancel: false,
        confirmColor: "#db3c40"
      })
    }else{
      that.editPhone();
      
    }
  },

  //点击获取验证码倒计时
  getVerificationCode() {
    var that = this;
    that.setData({
      disabled: true
    });
    that.getverify();
    that.changeCodeNum("true");
  },
  changeCodeNum: function () {
    let that = this,
      codeTime = that.data.codeTime;
    let clock = setInterval(() => {
      codeTime--;
      that.setData({
        codeText: codeTime + 's'
      });
      if (codeTime >= 0) {
        that.setData({
          codeTime: codeTime
        })
      } else {
        clearInterval(clock)
        that.setData({
          codeTime: 60,
          mobileStatus: false,
          codeText: '获取验证码'
        })
      }
    }, 1000)
  },
  //输入验证码
  getCodeInput: function (e) {
    let that = this;
    let phoneCode = e.detail.value;
    let verifyCode = that.data.verifyCode;
    console.log(verifyCode)
    that.setData({
      phoneCode: phoneCode,
      zhengLove: false,
    })
    if (phoneCode.length > 6) {
      if (phoneCode == verifyCode) {
        that.setData({
          zhengLove: true,
        })
      } else {
        that.setData({
          zhengLove: false,
        })
        wx.showModal({
          content: '输入验证码有误',
          showCancel: false,
          success: function (res) {}
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getuserInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  //个人信息
  getuserInfo: function () {
    var that = this;
    let uid=wx.getStorageSync('userid');
    var param = {
      uid:uid.id,
      user_type: uid.user_type
    };
    Api.ajax("/app/Zpersonalcenter/userInfo", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          userInfo: res.data
        })
      }
    })
  },
  //用户修改绑定手机接口
  editPhone: function () {
    // console.log(e)
    var that = this;
    var param = {
      type: 1,
      yzm: that.data.phoneCode,
      mobile: that.data.userInfo.mobile,
    }
    console.log(param)
    Api.ajax("/app/Zaccount/editPhone", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '验证成功', //这里成功
          icon: 'success',
          duration: 1000
        });
        wx.navigateTo({
          url: '/pages/user/user/bindTelStepTwo/bindTelStepTwo',
        })
      }else{
        wx.showToast({
          title: '验证码错误', //这里失败
          icon: 'none',
          duration: 1000
        });
      } 
    })
  },
  //获取验证码接口
  getverify: function () {
    var that = this;
    var param = {
      mobile:that.data.userInfo.mobile,
      type: 2,
    }
    console.log(param)
    Api.ajax("/app/Zaccount/yzm", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '获取成功', //这里成功
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})